package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.codehaus.jettison.util.FastStack;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;


    @Transactional
    public void saveWithFlavor(DishDTO dto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto, dish);
        //向菜品表插入一条数据 ，每次只能添加一个菜品
        dishMapper.insert(dish);

        List<DishFlavor> flavors = dto.getFlavors();
        if (!flavors.isEmpty()) {
            flavors.forEach(s -> s.setDishId(dish.getId())); //遍历集合，将菜品id放到每一个口味表中
            //口味可以多重，向口味表插入n条数据
            dishFlavorMapper.insertbatch(flavors);
        }

    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());

        Page<DishVO> page = dishFlavorMapper.pagequery(dishPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 菜品的批量删除
     *
     * @param ids
     */
    @Transactional
    @Override
    public void deletebatch(List<Long> ids) {

        //判断当前菜品是否起售，起售无法删除
        for (Long id : ids) {
            Dish dish = dishMapper.getbyid(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //判断菜品是否在套餐中，在即无法删除
        List<Long> setmealids = setmealDishMapper.getsetmealdishid(ids);
        if (setmealids != null && !setmealids.isEmpty()){ //判断菜品查询有没有带套餐
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        //删除菜品表内数据
        dishMapper.deletebatch(ids);
        //菜品口味一并删除
        dishFlavorMapper.deletebydishid(ids);
    }


    public List<DishVO> getbyidwithflavor(Dish dish) {

        List<Dish> getbyid = dishMapper.list(dish); //获取菜品信息 //更改为动态获取，可以是多个菜品id
        List<DishVO> dishVOlist = new ArrayList<>();

        for (Dish d : getbyid) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            List<DishFlavor> flavors =  dishFlavorMapper.getbyfalvor(d.getId()); //获取菜品的口味信息 ，使用菜品id查询
            dishVO.setFlavors(flavors);//封装到vo中
            dishVOlist.add(dishVO); //循环菜品，一次封装到集合中
        }

        return dishVOlist;
    }


    public void updatewithflavor(DishDTO dto) {
        //删除原始对象
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto,dish);
        dishMapper.update(dish);

        //口味修改比较麻烦，因此，修改底层默认将原始数据全部删掉，然后重新上传数据，如果没有数据就不上传
        List<Long> list = new ArrayList<>();
        list.add(dto.getId());
        dishFlavorMapper.deletebydishid(list);

        //删除原口味后，重新增加口味信息
        List<DishFlavor> dishFlavors = dto.getFlavors();
        if (!dishFlavors.isEmpty()) {
            dishFlavors.forEach(s -> s.setDishId(dto.getId())); //遍历集合，将菜品id放到每一个口味表中的dishid上
            //口味可以多重，向口味表插入n条数据
            dishFlavorMapper.insertbatch(dishFlavors);
        }

    }

    /**
     * 查询分类id的口味信息
     * @param categoryId
     * @return
     */

    public List<Dish> list(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }

    /**
     * 更改菜品状态
     * @param status
     * @param id
     */
    public void statusorstop(Integer status, Long id) {
        Dish build = Dish.builder()
                .id(id)
                .status(status)
                .build();
        dishMapper.update(build);
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            /*List<Long> ids = new ArrayList<>();
            ids.add(d.getId());*/

            List<DishFlavor> flavors = dishFlavorMapper.getbyfalvor(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

}
